package com.wangyb.wework.wxApi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wangyb.wework.entity.contact.WwContact;
import com.wangyb.wework.entity.contact.WwContactEmployee;
import com.wangyb.wework.entity.contact.WwContactTagPivot;
import lombok.Data;

import java.util.*;

import static com.wangyb.wework.common.WxApiConst.API_GET_EXTERNAL_INFO;

/**
 * @Author: wangyb
 * @Date: 2021/6/30 11:46 下午
 */
@Data
public class ExternalContactResp extends BaseWxResp {
	private Long corpId;
	private JSONObject externalContact;
	private JSONArray followUser;
	private String nextCursor;

	public WwContact getContact(){
		WwContact contact = new WwContact();
		contact.setAvatar(externalContact.getString("avatar"));
		contact.setName(externalContact.getString("name"));
		contact.setUnionid(externalContact.getString("unionid"));
		contact.setPosition(externalContact.getString("position"));
		contact.setWxExternalUserid(externalContact.getString("external_userid"));
		contact.setType(externalContact.getInteger("type"));
		contact.setGender(externalContact.getInteger("gender"));
		contact.setCorpFullName(externalContact.getString("corp_full_name"));
		contact.setCorpName(externalContact.getString("corp_name"));
		contact.setExternalProfile(externalContact.getString("external_profile"));

		contact.setCorpId(corpId);
		contact.setBusinessNo("");
		contact.setFollowUpStatus(0);
		contact.setNickName("");

		return contact;
	}
	public List<WwContactEmployee> getContactEmployeeList(){
		List<WwContactEmployee> list = new ArrayList<>();
		for (int i=0; i<followUser.size();i++){
			JSONObject followUserItem = followUser.getJSONObject(i);
			WwContactEmployee contactEmployee = new WwContactEmployee();

			String wxUserId = followUserItem.getString("userid");
			contactEmployee.setEmployeeWxUserid(wxUserId);
			contactEmployee.setRemark(followUserItem.getString("remark"));
			contactEmployee.setDescription(followUserItem.getString("description"));
			contactEmployee.setRemarkCorpName(followUserItem.getString("remark_corp_name"));
			contactEmployee.setRemarkMobiles(followUserItem.getString("remark_mobiles"));
			contactEmployee.setAddWay(followUserItem.getInteger("add_way"));
			contactEmployee.setAddTime(new Date(followUserItem.getLong("createtime") * 1000));
			contactEmployee.setOperUserid(followUserItem.getString("oper_userid"));
			contactEmployee.setState(followUserItem.getString("state"));
			// contactEmployee.setContactId(contactId);
			contactEmployee.setCorpId(corpId);
			//contactEmployee.setEmployeeId(empId);
//			contactEmployee.setStatus(1);

			//开始处理tags
			List<WwContactTagPivot> tagPivotList = new ArrayList<>();
			JSONArray tags = followUserItem.getJSONArray("tags");
			for (int l = 0; l < tags.size(); l++) {
				JSONObject tag = tags.getJSONObject(l);
				//仅企业设置（type为1）的标签返回
				String tagId = tag.getString("tag_id");
				WwContactTagPivot contactTagPivot = new WwContactTagPivot();
				contactTagPivot.setType(tag.getInteger("type"));
				contactTagPivot.setWxUserId(wxUserId);
				contactTagPivot.setWxTagId(tagId);
				contactTagPivot.setGroupName(tag.getString("group_name"));
				contactTagPivot.setTagName(tag.getString("tag_name"));
				tagPivotList.add(contactTagPivot);
			}
			contactEmployee.setTagPivotList(tagPivotList);
			list.add(contactEmployee);
		}
		return list;
	}
	public WwContactEmployee getFirstAddEmployee(){
		WwContactEmployee contactEmployee = Collections.min(this.getContactEmployeeList(), Comparator.comparing(ce->ce.getAddTime()));;
		return contactEmployee;
	}
	public WwContactEmployee getContactEmployee(String employeeWxUserid){
		return this.getContactEmployeeList().stream()
			.filter(ce -> ce.getEmployeeWxUserid().equals(employeeWxUserid))
			.findFirst()
			.get();
	}

	@Override
	public  String reqUrl() {
		return API_GET_EXTERNAL_INFO;
	}
}
